package m.app.action;

import m.base.model.AppInfo;
import m.base.model.StoreInfo;
import m.base.model.UserInfo;
import m.base.service.StoreInfoService;
import m.base.service.UserInfoService;
import m.base.util.PowerUtil;
import m.common.action.ActionMeta;
import m.common.action.RequestMeta;
import m.common.model.type.FieldType;
import m.common.model.util.ModelQueryCheck;
import m.common.model.util.ModelQueryList;
import m.common.model.util.ModelUpdateUtil;
import m.common.model.util.QueryCondition;
import m.store.model.GuestInfo;
import m.store.service.GuestInfoService;
import m.store.service.RepositoryInfoService;
import m.system.cache.CacheUtil;
import m.system.document.DocumentMeta;
import m.system.document.DocumentMethodMeta;
import m.system.document.DocumentModelMeta;
import m.system.document.DocumentParamMeta;
import m.system.exception.MException;
import m.system.util.GenerateID;
import m.system.util.JSONMessage;
import m.system.util.StringUtil;
import manage.model.AdminGroup;
import manage.model.StatusModel;

import java.util.List;

@ActionMeta(name = "app/storeInfo",title = "app商户信息")
public class StoreInfoAction extends AppAction{
    private StoreInfo model;
    private UserInfo user;
    @DocumentMeta(
        method = @DocumentMethodMeta(title = "商户申请")
    )
    @RequestMeta(transaction = true)
    public JSONMessage applyStore() throws Exception {
        List<AdminGroup> groupList=ModelQueryList.getModelList(AdminGroup.class,new String[]{"oid","name"},null,
            QueryCondition.and(
                QueryCondition.eq("business","B"),
                QueryCondition.eq("type","C"),
                QueryCondition.eq("status", StatusModel.Status_Enable),
                QueryCondition.isEmpty("parent.oid")
            )
        );
        if(groupList.isEmpty()) throw new MException(this.getClass(),"没有默认区域，请联系管理员");
        model=new StoreInfo();
        model.setParent(groupList.get(0));
        model.setName("商户"+ GenerateID.upsetKey(GenerateID.tempKey()));
        model.setTel("未知");
        model.setAddress("未知");
        user=new UserInfo();
        user.setUsername(GenerateID.upsetKey(GenerateID.generatePrimaryKey()));
        user.setPassword(GenerateID.upsetKey(GenerateID.generatePrimaryKey()));
        user.setRealname("老板");
        user.setPhone("未知");
//        ModelQueryCheck.init(model)
//                .checkNotNull("name","请输入商户名称")
//                .checkNotNull("parent.oid","请选择区域")
//                .doCheck(false,true);
//        ModelQueryCheck.init(user)
//                .checkNotNull("username","请输入员工号")
//                .checkNotNull("password","请输入登录码")
//                .checkNotNull("realname","请输入员工姓名")
//                .doCheck(false,true);
        AppInfo app= CacheUtil.get(AppInfo.class,"");
        if(StringUtil.notSpace(app.getRegisterCode())){
            throw new MException(this.getClass(),"无法注册，请联系管理员。");
        }
        model=getService(StoreInfoService.class).addStoreInfo(model);
        user=getService(UserInfoService.class).addUser(user,model,user.getPassword());
        model.setCreateUserOid(user.getOid());
        ModelUpdateUtil.updateModel(model,"createUserOid");
        getService(UserInfoService.class).setOperType(user.getOid(),model.getOid(),new String[]{UserInfo.OperType_Store,UserInfo.OperType_Finance,UserInfo.OperType_Stock,UserInfo.OperType_Collect});
        getService(GuestInfoService.class).addSelf(model,user);
        getService(RepositoryInfoService.class).addDefault(model,user);
        return new JSONMessage(0,"申请成功")
                .push("user",user);
    }
    @DocumentMeta(
        method = @DocumentMethodMeta(title = "查询店铺信息",permission = true)
    )
    public JSONMessage getInfo() throws Exception {
        model= ModelQueryList.getModel(StoreInfo.class,PowerUtil.getSessionStoreOid(),"*");
        return JSONMessage.success("查询成功")
                .push("model",model);
    }

    public StoreInfo getModel() {
        return model;
    }

    public void setModel(StoreInfo model) {
        this.model = model;
    }

    public UserInfo getUser() {
        return user;
    }

    public void setUser(UserInfo user) {
        this.user = user;
    }
}
